<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ page import="com.bookmanager.model.UserProfile" %>
<%@ page import="com.bookmanager.model.User" %>
<%
    User user = (User) session.getAttribute("user");
    if (user == null) {
        response.sendRedirect("login.jsp");
        return;
    }

    UserProfile userProfile = (UserProfile) request.getAttribute("userProfile");
    if (userProfile == null) {
        response.sendRedirect("dashboard?message=profile_not_found");
        return;
    }
%>
<html>
<head>
    <title>个人信息 - 图书管理系统</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }

        body {
            font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
            background: #f5f7fa;
            color: #333;
        }

        .header {
            background: white;
            box-shadow: 0 2px 10px rgba(0,0,0,0.1);
            padding: 0 20px;
        }

        .nav-bar {
            display: flex;
            justify-content: space-between;
            align-items: center;
            max-width: 1200px;
            margin: 0 auto;
            height: 70px;
        }

        .logo {
            font-size: 1.5rem;
            font-weight: bold;
            color: #667eea;
        }

        .user-info {
            display: flex;
            align-items: center;
            gap: 15px;
        }

        .welcome {
            color: #666;
        }

        .logout-btn {
            padding: 8px 16px;
            background: #dc3545;
            color: white;
            text-decoration: none;
            border-radius: 5px;
            font-size: 0.9rem;
        }

        .container {
            max-width: 1000px;
            margin: 30px auto;
            padding: 0 20px;
        }

        .page-header {
            text-align: center;
            margin-bottom: 40px;
        }

        .page-header h1 {
            color: #333;
            margin-bottom: 10px;
        }

        .page-header p {
            color: #666;
            font-size: 1.1rem;
        }

        .profile-section {
            background: white;
            border-radius: 10px;
            padding: 30px;
            margin-bottom: 30px;
            box-shadow: 0 5px 15px rgba(0,0,0,0.1);
        }

        .section-header {
            display: flex;
            justify-content: space-between;
            align-items: center;
            margin-bottom: 20px;
            padding-bottom: 15px;
            border-bottom: 2px solid #f0f0f0;
        }

        .section-title {
            font-size: 1.3rem;
            color: #333;
        }

        .profile-grid {
            display: grid;
            grid-template-columns: 1fr 1fr;
            gap: 30px;
        }

        .profile-card {
            background: #f8f9fa;
            border-radius: 10px;
            padding: 25px;
        }

        .profile-avatar {
            text-align: center;
            margin-bottom: 20px;
        }

        .avatar-icon {
            font-size: 4rem;
            color: #667eea;
        }

        .user-name {
            text-align: center;
            font-size: 1.5rem;
            font-weight: bold;
            margin-bottom: 5px;
            color: #333;
        }

        .user-role {
            text-align: center;
            color: #666;
            margin-bottom: 20px;
        }

        .info-grid {
            display: grid;
            gap: 15px;
        }

        .info-item {
            display: flex;
            justify-content: space-between;
            padding: 10px 0;
            border-bottom: 1px solid #e9ecef;
        }

        .info-label {
            color: #666;
            font-weight: 500;
        }

        .info-value {
            color: #333;
            font-weight: 600;
        }

        .stats-grid {
            display: grid;
            grid-template-columns: repeat(auto-fit, minmax(150px, 1fr));
            gap: 15px;
            margin-top: 20px;
        }

        .stat-card {
            background: white;
            padding: 20px;
            border-radius: 8px;
            text-align: center;
            box-shadow: 0 2px 5px rgba(0,0,0,0.1);
        }

        .stat-number {
            font-size: 1.8rem;
            font-weight: bold;
            color: #667eea;
            margin-bottom: 5px;
        }

        .stat-label {
            color: #666;
            font-size: 0.9rem;
        }

        .btn-back {
            background: #667eea;
            color: white;
            padding: 12px 24px;
            text-decoration: none;
            border-radius: 5px;
            display: inline-block;
            margin-top: 20px;
            border: none;
            cursor: pointer;
            font-size: 14px;
        }

        .btn-back:hover {
            background: #5a6fd8;
        }

        .action-buttons {
            display: flex;
            gap: 15px;
            margin-top: 25px;
        }

        .btn {
            padding: 10px 20px;
            border: none;
            border-radius: 5px;
            cursor: pointer;
            text-decoration: none;
            display: inline-block;
            text-align: center;
            font-size: 14px;
        }

        .btn-primary {
            background: #28a745;
            color: white;
        }

        .btn-secondary {
            background: #6c757d;
            color: white;
        }

        @media (max-width: 768px) {
            .profile-grid {
                grid-template-columns: 1fr;
            }

            .stats-grid {
                grid-template-columns: repeat(2, 1fr);
            }
        }
    </style>
</head>
<body>
<!-- 顶部导航 -->
<div class="header">
    <div class="nav-bar">
        <div class="logo">📚 图书管理系统</div>
        <div class="user-info">
            <span class="welcome">欢迎，<strong><%= user.getUsername() %></strong></span>
            <a href="<%= request.getContextPath() %>/logout" class="logout-btn">退出登录</a>
        </div>
    </div>
</div>

<div class="container">
    <!-- 页面头部 -->
    <div class="page-header">
        <h1>👤 个人信息</h1>
        <p>查看和管理您的个人账户信息</p>
    </div>

    <!-- 个人信息 -->
    <div class="profile-section">
        <div class="section-header">
            <h3 class="section-title">基本信息</h3>
        </div>

        <div class="profile-grid">
            <!-- 左侧：头像和基本信息 -->
            <div class="profile-card">
                <div class="profile-avatar">
                    <div class="avatar-icon">👤</div>
                </div>
                <div class="user-name"><%= userProfile.getUsername() %></div>
                <div class="user-role">
                    <% if ("admin".equals(userProfile.getRole())) { %>
                    <span style="background: #28a745; color: white; padding: 4px 8px; border-radius: 12px; font-size: 0.8rem;">管理员</span>
                    <% } else { %>
                    <span style="background: #667eea; color: white; padding: 4px 8px; border-radius: 12px; font-size: 0.8rem;">普通用户</span>
                    <% } %>
                </div>

                <div class="info-grid">
                    <div class="info-item">
                        <span class="info-label">用户ID:</span>
                        <span class="info-value"><%= userProfile.getUserId() %></span>
                    </div>
                    <div class="info-item">
                        <span class="info-label">注册时间:</span>
                        <span class="info-value"><%= userProfile.getCreateTime() != null ? userProfile.getCreateTime().toLocalDate().toString() : "未知" %></span>
                    </div>
                </div>
            </div>

            <!-- 右侧：联系信息 -->
            <div class="profile-card">
                <h4 style="margin-bottom: 20px; color: #333;">联系信息</h4>
                <div class="info-grid">
                    <div class="info-item">
                        <span class="info-label">邮箱:</span>
                        <span class="info-value">
                            <%= userProfile.getEmail() != null && !userProfile.getEmail().isEmpty() ? userProfile.getEmail() : "未设置" %>
                        </span>
                    </div>
                    <div class="info-item">
                        <span class="info-label">手机号:</span>
                        <span class="info-value">
                            <%= userProfile.getPhone() != null && !userProfile.getPhone().isEmpty() ? userProfile.getPhone() : "未设置" %>
                        </span>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <!-- 统计信息 -->
    <div class="profile-section">
        <div class="section-header">
            <h3 class="section-title">阅读统计</h3>
        </div>

        <div class="stats-grid">
            <div class="stat-card">
                <div class="stat-number"><%= userProfile.getBorrowCount() %></div>
                <div class="stat-label">总借阅量</div>
            </div>
            <div class="stat-card">
                <div class="stat-number"><%= userProfile.getCurrentBorrows() %></div>
                <div class="stat-label">当前借阅</div>
            </div>
            <div class="stat-card">
                <div class="stat-number"><%= userProfile.getFavoriteCount() %></div>
                <div class="stat-label">收藏图书</div>
            </div>
            <div class="stat-card">
                <div class="stat-number">
                    <%= userProfile.getCreateTime() != null ?
                            java.time.temporal.ChronoUnit.DAYS.between(userProfile.getCreateTime().toLocalDate(), java.time.LocalDate.now()) : 0 %>
                </div>
                <div class="stat-label">使用天数</div>
            </div>
        </div>
    </div>

    <!-- 操作按钮 -->
    <div style="text-align: center;">
        <div class="action-buttons" style="justify-content: center;">
            <a href="<%= request.getContextPath() %>/dashboard" class="btn btn-secondary">返回用户中心</a>
            <button class="btn btn-primary" onclick="editProfile()">编辑信息</button>
        </div>
    </div>
</div>

<script>
    function editProfile() {
        alert('编辑个人信息功能正在开发中...');
        // 后续可以跳转到编辑页面
        // window.location.href = 'profile-edit.jsp';
    }
</script>
</body>
</html>
